class Character {
    constructor(id, name, stats, trait) {
        this.id = id;
        this.name = name;

        // 基础属性
        this.maxHp = stats.maxHp || 100;
        this.hp = this.maxHp;
        this.defense = stats.defense || 10;
        this.attack = stats.attack || 15;

        // 特性
        this.trait = trait || {
            name: '无',
            description: '该角色没有特性'
        };

        // 图片路径
        this.images = {
            full: `/character/${id}/full.png`,    // 全身立绘
            card: `/character/${id}/card.png`     // 卡牌图片
        };

        // Buff/Debuff系统
        this.buffs = new Map();

        // 技能列表
        this.skills = new Set();

        // 金币和经验
        this.gold = 0;      // 金币
        this.exp = 0;       // 当前经验
        this.maxExp = 1000; // 升级所需经验
        this.level = 1;     // 等级
        this.inventory = []; // 背包
    }

    // 添加buff
    addBuff(buffId, buffData) {
        this.buffs.set(buffId, buffData);
    }

    // 移除buff
    removeBuff(buffId) {
        this.buffs.delete(buffId);
    }

    // 更新buff持续时间
    updateBuffs() {
        for (const [buffId, buffData] of this.buffs.entries()) {
            buffData.duration--;
            if (buffData.duration <= 0) {
                this.removeBuff(buffId);
            }
        }
    }

    // 获取当前防御力（包含buff加成）
    getCurrentDefense() {
        let totalDefense = this.defense;
        for (const buffData of this.buffs.values()) {
            if (buffData.type === 'defense') {
                totalDefense += buffData.value;
            }
        }
        return totalDefense;
    }

    // 受到伤害
    takeDamage(damage) {
        const actualDefense = this.getCurrentDefense();
        const reducedDamage = Math.max(1, damage - actualDefense);
        this.hp = Math.max(0, this.hp - reducedDamage);
        return reducedDamage;
    }

    // 治疗
    heal(amount) {
        const oldHp = this.hp;
        this.hp = Math.min(this.maxHp, this.hp + amount);
        return this.hp - oldHp;
    }

    // 判断是否存活
    isAlive() {
        return this.hp > 0;
    }

    // 特性相关方法
    applyTrait(context) {
        // 由具体的角色类实现
    }

    // 节点移动特性
    onNodeMove(fromNode, toNode) {
        // 默认移动特性：回复3%的生命值
        const healAmount = Math.floor(this.maxHp * 0.03);
        this.heal(healAmount);
        console.log(`${this.name}移动到新节点，回复${healAmount}点生命值`);
    }

    addGold(amount) {
        this.gold += amount;
    }

    addExp(amount) {
        this.exp += amount;
        if (this.exp >= this.maxExp) {
            this.levelUp();
        }
    }

    levelUp() {
        this.level++;
        this.exp -= this.maxExp;
        this.maxExp = Math.floor(this.maxExp * 1.5);
        // TODO: 升级时属性提升
    }
}

export default Character;